package com.usian.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.usian.AdNode;
import com.usian.PageResult;
import com.usian.mapper.TbContentCategoryMapper;
import com.usian.mapper.TbContentMapper;
import com.usian.pojo.TbContent;
import com.usian.pojo.TbContentCategory;
import com.usian.pojo.TbContentCategoryExample;
import com.usian.pojo.TbContentExample;
import com.usian.redis.RedisClient;
import com.usian.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ContentServiceImpl implements ContentService {

    @Autowired
    private TbContentCategoryMapper tbContentCategoryMapper;
    @Autowired
    private TbContentMapper tbContentMapper;
    @Autowired
    private RedisClient redisClient;

    @Value("${AD_CATEGORY_ID}")
    private Long AD_CATEGORY_ID;

    @Value("${AD_HEIGHT}")
    private Integer AD_HEIGHT;

    @Value("${AD_WIDTH}")
    private Integer AD_WIDTH;

    @Value("${AD_HEIGHTB}")
    private Integer AD_HEIGHTB;

    @Value("${AD_WIDTHB}")
    private Integer AD_WIDTHB;

    @Value("${PORTAL_AD_KEY}")
    private String PORTAL_AD_KEY;

    @Override
    public List<TbContentCategory> selectContentCategoryByParentId(Long id) {
        TbContentCategoryExample example =new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(id);
        criteria.andStatusEqualTo(1);
        List<TbContentCategory> categoryList = tbContentCategoryMapper.selectByExample(example);
        return categoryList;
    }

    public  Integer insertContentCategory(Long parentId, String name){


        TbContentCategory content = new TbContentCategory();
        content.setParentId(parentId);
        content.setName(name);
        content.setSortOrder(1);
        content.setStatus(1);
        content.setCreated(new Date());
        content.setUpdated(new Date());
        content.setIsParent(false); //默认儿子
        int num = tbContentCategoryMapper.insertSelective(content);


        TbContentCategoryExample example =new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria =example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        TbContentCategory category = tbContentCategoryMapper.selectByPrimaryKey(parentId);

        //2.2:断是不是父节点  不是  改成父节点
        if(!category.getIsParent()){
            category.setIsParent(true);
            category.setUpdated(new Date());
            int num1 = tbContentCategoryMapper.updateByPrimaryKeySelective(category);
        }

        return num;
    }


    public Integer deleteContentCategoryById(Long categoryId){


        TbContentCategory category = tbContentCategoryMapper.selectByPrimaryKey(categoryId);

        if(category.getIsParent()){
            return 0;
        }
        TbContentCategory contentCategory =new TbContentCategory();
        contentCategory.setId(categoryId);
        contentCategory.setStatus(2);
        contentCategory.setUpdated(new Date());
        tbContentCategoryMapper.updateByPrimaryKeySelective(contentCategory);

        //根据父节点id 查询 下面还有多少个子节点
        TbContentCategoryExample example =new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria =example.createCriteria();
        criteria.andParentIdEqualTo(category.getParentId());
        criteria.andStatusEqualTo(1);
        List<TbContentCategory> list = tbContentCategoryMapper.selectByExample(example);

        if(list.size()==0){
            TbContentCategory parenttbContentCategory = new TbContentCategory();
            parenttbContentCategory.setId(category.getParentId());
            parenttbContentCategory.setIsParent(false);
            parenttbContentCategory.setUpdated(new Date());
            tbContentCategoryMapper.updateByPrimaryKeySelective(parenttbContentCategory);
        }

        return 200;
    }

    @Override
    public PageResult selectTbContentAllByCategoryId(Long categoryId, Integer page, Integer rows) {

        PageHelper.startPage(page,rows);
        TbContentExample example = new TbContentExample();
        TbContentExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryIdEqualTo(categoryId);
        List<TbContent> list = tbContentMapper.selectByExample(example);

        PageInfo<TbContent> pageInfo =new PageInfo<>(list);

        PageResult  pageResult =new PageResult();
        pageResult.setPageIndex(pageInfo.getPageNum());
        pageResult.setTotalPage(pageInfo.getPages());
        pageResult.setResult(pageInfo.getList());

        return pageResult;
    }

    @Override
    public int insertTbContent(TbContent tbContent) {
        tbContent.setUpdated(new Date());
        tbContent.setCreated(new Date());
        redisClient.hdel(PORTAL_AD_KEY, AD_CATEGORY_ID.toString());
        return tbContentMapper.insertSelective(tbContent);
    }

    @Override
    public int deleteContentByIds(Long ids) {
        redisClient.hdel(PORTAL_AD_KEY, AD_CATEGORY_ID.toString());
        return tbContentMapper.deleteByPrimaryKey(ids);
    }

    @Override
    public List<AdNode> selectFrontendContentByAD() {


        //先到redis查，没有再访问数据库
        List<AdNode> adNodeList1  =(List<AdNode>) redisClient.hget(PORTAL_AD_KEY, AD_CATEGORY_ID.toString());
        if(adNodeList1!=null && adNodeList1.size()>0){
            return adNodeList1;
        }

        //1：id=89
        TbContentExample example = new TbContentExample();
        TbContentExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryIdEqualTo(AD_CATEGORY_ID);
        List<TbContent> list = tbContentMapper.selectByExample(example);

        //2:content list 遍历 赋值 adnode  add  list
        List<AdNode> adNodeList = new ArrayList<>();
        if(list.size()>0){
            for(TbContent content:list){
                AdNode adNode =new AdNode();
                adNode.setSrc(content.getPic());
                adNode.setSrcB(content.getPic2());
                adNode.setHref(content.getUrl());
                adNode.setHeight(AD_HEIGHT);
                adNode.setWidth(AD_WIDTH);
                adNode.setHeightB(AD_HEIGHTB);
                adNode.setWidthB(AD_WIDTHB);
                adNodeList.add(adNode);
            }
        }

        //首页广告缓存到redis 使用hash类型
        redisClient.hset(PORTAL_AD_KEY,AD_CATEGORY_ID.toString(),adNodeList);
        return adNodeList;
    }


}
